package com.taomu.app.zongchouhuli;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.taomu.app.zongchouhuli.http.api.ApiService;
import com.taomu.app.zongchouhuli.http.api.NetWork;
import com.taomu.app.zongchouhuli.moudle.index.adpter.BaseViewAdpter;
import com.taomu.app.zongchouhuli.moudle.index.fragment.MineIndexFragment;
import com.taomu.app.zongchouhuli.moudle.index.fragment.ProductIndexFragment;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.utils.dialog.PayPasswordDialog;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;

    private BottomNavigationView mBottomView;

    private BaseViewAdpter mBaseViewAdpter;

    private MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //隐藏标题栏
        getSupportActionBar().hide();
        //全屏，去掉状态栏
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        DpUtils.init(this);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mViewPager = findViewById(R.id.viewpager);
        mBottomView = findViewById(R.id.navigation_view);
        mBaseViewAdpter = new BaseViewAdpter(getSupportFragmentManager());
        mViewPager.setAdapter(mBaseViewAdpter);
        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(ProductIndexFragment.getInstence());
        mFragments.add(MineIndexFragment.getInstence());
        mBaseViewAdpter.setFragment(mFragments);
    }

    private void initEvent() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    mBottomView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = mBottomView.getMenu().getItem(i);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mBottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                menuItem = item;
                switch (item.getItemId()) {
                    case R.id.navigation_product:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.navigation_mine:
                        mViewPager.setCurrentItem(1);
                        break;
                }
                return false;
            }
        });
    }

    private void initData() {

    }

    private static boolean isExit = false;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, "再按一退出程序", Toast.LENGTH_SHORT).show();
            //利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }
}
